#!/usr/bin/python

'''
Created on Jul 31, 2013
need to set redirect args after the cmd line
'''

import os
import time

filepath = '/home/zhc/'
filetype = 'text'
fileto = "/home/zhc/temporary/info.txt"
txt = 'subversion.hai'
isdebug = True 

stime = time.time()
print time.strftime('%Y-%m-%d %H:%I:%M.%S',time.localtime(stime))

for root, dirs, files in os.walk(filepath, True):
    for f in files:
        afile = os.path.join(root, f)
        afile = afile.replace(' ', '\ ')
        afile = afile.replace('$', '\$')
        afile = afile.replace('(', '\(')
        afile = afile.replace(')', '\)')
        retStr = os.popen("file "  + afile).read()
        if(isdebug):
            print retStr
        if(retStr.find(filetype)!=-1):
            greStr = os.popen("grep " + txt + " " + afile).read()
            if(len(greStr)!=0):
                os.system("echo > " + fileto)
                os.system("echo " + afile + ">> " + fileto)
                print afile

etime = time.time()
atime = etime - stime

print "start time: ", time.strftime('%Y-%m-%d %H:%I:%M.%S',time.localtime(stime))
print "end time:", time.strftime('%Y-%m-%d %H:%I:%M.%S',time.localtime(etime))
print "expend time: ", atime, "s"

